package com.osdp.base.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.osdp.base.util.Fields;
import com.osdp.base.entity.SysDictItem;
import com.osdp.base.mapper.SysDictItemMapper;
import com.osdp.base.service.ISysDictItemService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.List;

@Service
@Transactional
public class SysDictItemServiceImpl extends ServiceImpl<SysDictItemMapper, SysDictItem> implements ISysDictItemService {
    @Override
    public IPage<SysDictItem> pageList(SysDictItem dictItem, Page<SysDictItem> page) {
        return this.page(page, new QueryWrapper<SysDictItem>()
                .eq(StringUtils.isNotBlank(dictItem.getDictItemCode()), Fields.SysDictItemQ.dictItemCode, dictItem.getDictItemCode())
                .like(StringUtils.isNotBlank(dictItem.getItemValue()), Fields.SysDictItemQ.itemValue, dictItem.getItemValue())
                .like(StringUtils.isNotBlank(dictItem.getItemText()), Fields.SysDictItemQ.itemText, dictItem.getItemText())
                .eq(StringUtils.isNotBlank(dictItem.getParentId()), Fields.SysDictItemQ.parentId, dictItem.getParentId())
                .orderByDesc(Fields.SysDictItemQ.sortNo));
    }

    @Override
    public List<SysDictItem> queryTree() {
        return this.list(new QueryWrapper<SysDictItem>()
                .eq(Fields.SysDictItemQ.parentId,SysDictItem.ROOT_PARENT_ID));
    }

    @Override
    public List<SysDictItem> queryByParentId(String parentId) {
        List<SysDictItem> resutlList = Arrays.asList();
        if(StringUtils.isNotBlank(parentId)) {
//            resutlList = super.baseMapper.queryByParentId(parentId);
        }
        return resutlList;
    }

    @Override
    public List<SysDictItem> queryAllList(SysDictItem dictItem) {
        LambdaQueryWrapper<SysDictItem> wrapper = new LambdaQueryWrapper<>();
        if(StringUtils.isNotBlank(dictItem.getDictItemCode()))
            wrapper.eq(SysDictItem::getDictItemCode, dictItem.getDictItemCode());
        if(StringUtils.isNotBlank(dictItem.getItemValue()))
            wrapper.like(SysDictItem::getItemValue, dictItem.getItemValue());
        if(StringUtils.isNotBlank(dictItem.getItemText()))
            wrapper.like(SysDictItem::getItemText, dictItem.getItemText());

        List<SysDictItem> result = this.list(wrapper);
//        if(null == result) return Collections.emptyList();
        return result;
    }

    @Override
    public SysDictItem getByDictItemId(String id) {
        return super.getById(id);
    }

    @Override
    public List<SysDictItem> getDictListByCode(String itemCode){
        return this.list(new QueryWrapper<SysDictItem>()
                .eq(StringUtils.isNotBlank(itemCode), Fields.SysDictItemQ.dictItemCode,itemCode));
    }

    @Override
    @Transactional
    public boolean insertDictItem(SysDictItem entity) {
        return super.save(entity);
    }

    @Override
    @Transactional
    public boolean updateDictItem(SysDictItem entity) {
        return super.updateById(entity);
    }

    @Override
    @Transactional
    public boolean deleteDictItem(String id) {
        return super.removeById(id);
    }

    @Override
    @Transactional
    public boolean deleteBatcah(List<String> ids) {
        return super.removeByIds(ids);
    }
}
